package com.itheima.web;

import com.itheima.bean.Setmeal;
import com.itheima.common.ParamPage;
import com.itheima.common.R;
import com.itheima.dto.SetmealDto;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;


    @GetMapping("/page")
    public R findpage(ParamPage paramPage) {
        return R.success(setmealService.findPage(paramPage));
    }
    @PostMapping
    public R add(@RequestBody SetmealDto setmealDto){
        if (setmealService.add(setmealDto)>0){
            return R.success("添加成功！");
        }
        return R.error("添加失败！");
    }
    @GetMapping("/{id}")
    public R findById(@PathVariable long id){
        SetmealDto setmealDto = setmealService.findById(id);
        if (setmealDto!=null){
            return R.success(setmealDto);
        }
        return R.error("查询失败！");
    }
    @PutMapping
    public R update(@RequestBody SetmealDto setmealDto){
        if (setmealService.update(setmealDto)>0){
            return R.success("修改成功！");
        }
        return R.error("修改失败！");
    }
    @DeleteMapping
    public R delete(@RequestParam List<Long> ids){
        if (setmealService.delete(ids)>0){
            return R.success("删除成功！");
        }
        return R.error("删除失败！");
    }
    @PostMapping("/status/{status}")
    public R updateStatus(@PathVariable int status,@RequestParam List<Long> ids){
        if (setmealService.updateStatus(status,ids)>0){
            return R.success("状态修改成功！");
        }
        return R.error("状态修改失败！");
    }
    @Cacheable(value = "Setmeal",key = "#categoryId+'_'+#status")
    @GetMapping("/list")
    public R list(Long categoryId,Integer status){
        List<Setmeal> list = setmealService.findByCid(categoryId, status);
        if (list!=null){
            return R.success(list);
        }
        return R.error("查询无套餐！");
    }
}
